//-----------------------------------------------------------------------
// <copyright file="a_RelProductDAOBase.cs" company="AACECOM">
//     Copyright (c) AACECOM. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using Microsoft.Practices.EnterpriseLibrary.Data;
using ThoiTrang.Entities;

namespace ThoiTrang.Data
{    
    public abstract class a_RelProductDAOBase
    {
        #region Common methods
        public virtual a_RelProduct Createa_RelProductFromReader(IDataReader reader)
        {
            a_RelProduct item = new a_RelProduct();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("RPID"))) item.RPID = (int)reader["RPID"];
                if (!reader.IsDBNull(reader.GetOrdinal("RpGrpID"))) item.RpGrpID = (int)reader["RpGrpID"];
                if (!reader.IsDBNull(reader.GetOrdinal("RpCatID"))) item.RpCatID = (int)reader["RpCatID"];
                if (!reader.IsDBNull(reader.GetOrdinal("ProdID"))) item.ProdID = (int)reader["ProdID"];
                if (!reader.IsDBNull(reader.GetOrdinal("StoreID"))) item.StoreID = (int)reader["StoreID"];
                if (!reader.IsDBNull(reader.GetOrdinal("DomainID"))) item.DomainID = (int)reader["DomainID"];
            }
            catch (Exception ex)
            {                
                throw ex;
            }
            return item;
        }
        #endregion
        
        #region Createa_RelProduct methods
            
        public virtual void Createa_RelProduct(a_RelProduct a_RelProduct)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_RelProducts_ins");
                
                database.AddInParameter(dbCommand, "@RpGrpID", DbType.Int32, a_RelProduct.RpGrpID);
                database.AddInParameter(dbCommand, "@RpCatID", DbType.Int32, a_RelProduct.RpCatID);
                database.AddInParameter(dbCommand, "@ProdID", DbType.Int32, a_RelProduct.ProdID);
                database.AddInParameter(dbCommand, "@StoreID", DbType.Int32, a_RelProduct.StoreID);
                database.AddInParameter(dbCommand, "@DomainID", DbType.Int32, a_RelProduct.DomainID);
                database.AddOutParameter(dbCommand, "@RPID", DbType.Int32, 0);
                
                database.ExecuteNonQuery(dbCommand);
                a_RelProduct.RPID = (int)database.GetParameterValue(dbCommand, "@RPID");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        #region Updatea_RelProduct methods
        
        public virtual void Updatea_RelProduct(a_RelProduct a_RelProduct)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_RelProducts_upd");            
                
                database.AddInParameter(dbCommand, "@RPID", DbType.Int32, a_RelProduct.RPID);
                database.AddInParameter(dbCommand, "@RpGrpID", DbType.Int32, a_RelProduct.RpGrpID);
                database.AddInParameter(dbCommand, "@RpCatID", DbType.Int32, a_RelProduct.RpCatID);
                database.AddInParameter(dbCommand, "@ProdID", DbType.Int32, a_RelProduct.ProdID);
                database.AddInParameter(dbCommand, "@StoreID", DbType.Int32, a_RelProduct.StoreID);
                database.AddInParameter(dbCommand, "@DomainID", DbType.Int32, a_RelProduct.DomainID);
                
                database.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
               throw ex;
            }
        }
        
        #endregion

        #region Deletea_RelProduct methods
        public virtual void Deletea_RelProduct(int rpid)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_RelProducts_del");
                
                database.AddInParameter(dbCommand, "@RPID", DbType.Int32, rpid);
                
                database.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
               throw ex;
            }
        }

        #endregion

        #region Geta_RelProduct methods
        
        public virtual a_RelProduct Geta_RelProduct(int rpid)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_RelProducts_sel");
                
                database.AddInParameter(dbCommand, "@RPID", DbType.Int32, rpid);
                
                a_RelProduct a_RelProduct =  null;
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    if (reader.Read())
                    {
                        a_RelProduct = Createa_RelProductFromReader(reader);
                        reader.Close();
                    }
                }
                return a_RelProduct;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        
        #endregion

        #region Geta_RelProductList methods
        public virtual a_RelProductCollection Geta_RelProductList(a_RelProductColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_RelProducts_selList");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                a_RelProductCollection a_RelProductCollection = new a_RelProductCollection();
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    while (reader.Read())
                    {
                        a_RelProduct a_RelProduct = Createa_RelProductFromReader(reader);
                        a_RelProductCollection.Add(a_RelProduct);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return a_RelProductCollection;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        
        public virtual a_RelProductCollection Geta_RelProductList(a_RelProductColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return Geta_RelProductList(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion
    }
}